package service;

import common.Code;
import common.Message;
import common.MessageType;
import common.User;
import dao.*;
import util.ControlUserCode;
import util.CreatCode;
import util.EmailSend;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    ServerSocket serverSocket=null;
    public Server(){
        try {
            serverSocket=new ServerSocket(10086);
            System.out.println("服务器正在监听10086端口");
            while(true){
                Socket socket=serverSocket.accept();
                //从客户端接收消息，登录或者注册
                ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
                Message ms=(Message) ois.readObject();
                //登录请求
                if(ms.getState()==MessageType.Login_Require){
                    User user=(User) ois.readObject();
                    Message ms1=new Message();//登录是否成功的消息
                    ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                    //从数据库中获取密码
                    if(user.getPwd().equals(PwdFromBase.pwd(user.getId()))){
                        System.out.println("密码正确");
                        if(ControlSCConnectionThread.getThread(user.getId())!=null){
                            ms1.setState(MessageType.Login_Repeat);
                            System.out.println("重复登录");
                        }else{
                            ms1.setState(MessageType.Longin_Success);
                            user=PwdFromBase.getInfo(user.getId());
                        }
                        //向客户端发送密码正确，登录成功的消息
                        oos.writeObject(ms1);
                        if(ms1.getState()==MessageType.Longin_Success) {
                            oos.writeObject(user);
                            //启动服务端与客户端保持通信的线程
                            ServerConnectionClient scc=new ServerConnectionClient(socket,user.getId(),user);
                            scc.start();
                            //将线程放入集合
                            ControlSCConnectionThread.addSCThread(user.getId(),scc);
                        }
                    }else{
                        ms1.setState(MessageType.Login_Fail);
                        oos.writeObject(ms1);
                        socket.close();
                    }
                }
                else if(ms.getState()==MessageType.Register_Require){
                    User user=(User) ois.readObject();
                    Message ms1=new Message();//注册是否成功的消息
                    ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                    if(PwdFromBase.pwd(user.getId())==null){//非空表示用户已存在
                        if(Code.judge(ControlUserCode.getCode(user.getId()),user.getCode())){
                            if (InsertUserToBase.Register(user)){
                                ms1.setState(MessageType.Register_Success);
                                //向客户端发送注册成功的消息
                                oos.writeObject(ms1);
                                oos.writeObject(user.getNumber());
                                //启动服务端与客户端保持通信的线程
                                ServerConnectionClient scc=new ServerConnectionClient(socket,user.getId(),PwdFromBase.getInfo(user.getId()));
                                scc.start();
                                //将线程放入集合
                                ControlSCConnectionThread.addSCThread(user.getId(),scc);
                            }
                            else{
                                System.out.println("数据库添加用户失败");
                                ms1.setState(MessageType.Register_Fail);
                                oos.writeObject(ms1);
                                socket.close();
                            }
                        }else{
                            System.out.println("数据库添加用户失败");
                            ms1.setState(MessageType.Register_Fail);
                            oos.writeObject(ms1);
                            socket.close();
                        }
                    }else{
                        ms1.setState(MessageType.Register_Fail);
                        oos.writeObject(ms1);
                        socket.close();
                    }
                }else if(ms.getState()== MessageType.Email_Send){
                    String receiver=(String)  ois.readObject();
                    String code= CreatCode.creatCode();
                    if(EmailSend.send(receiver,code)){
                        System.out.println("邮箱验证码发送成功");
                        ControlUserCode.addCode(receiver,new Code(code));
                        System.out.println(ControlUserCode.getCode(receiver).getCode());
                    }
                }else if(ms.getState()==MessageType.pwd_Forget){
                    User user=(User) ois.readObject();
                    Message ms1=new Message();//重置是否成功的消息
                    ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                    if(Code.judge(ControlUserCode.getCode(user.getId()),user.getCode())){
                        if(PwdReset.Reset(user.getId(),user.getPwd()))
                        {
                            ms1.setState(MessageType.pwd_ResetSuccess);
                        }
                    }else{
                        ms1.setState(MessageType.User_null);
                    }
                    oos.writeObject(ms1);
                }
            }
        } catch (Exception e) {
            System.out.println("服务器监听出错");
            e.printStackTrace();
        }finally {
            try {
                serverSocket.close();
            } catch (Exception e) {
                System.out.println("服务器关闭异常");
            }
        }
    }
}